package com.example.study.controller;

import com.example.study.entity.Student;
import com.example.study.entity.Teacher;
import com.example.study.service.IStudentService;
import com.example.study.service.ITeacherService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @description：
 * @author: liqiancheng
 * @createTime: 2021/7/26 11:48
 **/
@RestController
@RequestMapping("/stu")
public class StudentController {


    @Resource
    private IStudentService studentService;

    @PostMapping("/save")
    public String saveOrUpdate(@RequestBody Student student) throws JsonProcessingException {
        Student newStudent = studentService.save(student);
        return new ObjectMapper().writeValueAsString(newStudent);
    }

    @GetMapping("/delete")
    public String delete(@RequestParam int id) throws JsonProcessingException {
        studentService.delete(id);
        return new ObjectMapper().writeValueAsString("删除成功!");
    }

    @GetMapping("/listAll")
    public String listAll() throws JsonProcessingException {
        return new ObjectMapper().writeValueAsString(studentService.List());
    }
}
